<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!--   获取绝对路径 -->
<c:set var="ctx" value="${pageContext.request.contextPath }" />
<%@ include file="/common/include/include.jsp"%>
<div class="modal-dialog" style="width:100%">
    <div class="modal-content" style="min-height: 100%;">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" onclick="{parent.layer.closeAll();}"><i class="pci-cross pci-circle"></i></button>
            <h4 class="modal-title">给角色授权</h4>
        </div>

        <div class="modal-body">
            <ul id="treeDemo" class="ztree"></ul>
        </div>
    </div>
</div>

<script>
	var roleid = "${requestScope.roleid }";
	//树
	$(document).ready(function() {
		//初始化树
		var clientH = document.body.clientHeight-54;
		$(".lay-body").css("height",clientH+"px");
		onloadZTree();//加载树形图
	});
	
	//设置树形图的各个属性
	var setting = {
		//是否异步
		async : {
			enable:true,
		},
		//复选框
		check:{enable:true},
		//树形显示节点，不设置这个属性，便不会区分父节点和子节点
		data: {
			simpleData: {
				enable: true
			}
		}
	};
	

	//加载ztree  
	function onloadZTree() {
		var ztreeNodes;
		$.ajax({
			async : true, //是否异步  
			cache : false, //是否使用缓存  
			type : 'post', //请求方式,post  
			dataType : "json", //数据传输格式  
			url : "${ctx }/admin/menu/getMenuTree", //请求链接  
			error : function() {
				alert('亲，网络有点不给力呀！');
			},
			success : function(data) {
				ztreeNodes = eval("[" + data + "]"); //将string类型转换成json对象
				$.fn.zTree.init($("#treeDemo"), setting, ztreeNodes);
				//获取这个数的对象，treeDemo与这棵树的id相对应
				var zTree = $.fn.zTree.getZTreeObj("treeDemo");
				//表示 展开 全部节点
				zTree.expandAll(true);
			
				//默认勾选已有的权限
				var data = common_ajax.ajaxFunc("/admin/role/getRoleAuthByRoleid", {"roleid":roleid}, "json", null);
				if(data&&data.length>0){
					$.each(data,function(i,d){
						var node = zTree.getNodeByParam("id", d.id, null);//根据menuid获取节点
						if(node){
							zTree.checkNode(node, true, false, true);//将节点选中
						}
					})
				}
			}
		});
	};
	
	//获取选择的节点
	function getCheckedNodes(){
		var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
		var nodes = treeObj.getCheckedNodes(true);
		return nodes;
	}
</script>
